package tt.dz.biz.service.user;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.user.OfflineAccountDAO;
import tt.dz.biz.service.cardno.CardNoProduct;
import tt.dz.biz.service.common.CommonService;
import tt.dz.common.BusinessException;
import tt.dz.common.DateUtil;
import tt.dz.common.DzConstants;
import tt.dz.common.DzConstants.cardState;
import tt.dz.common.LogUtil;
import tt.dz.vo.OfflineAccountVO;

@Service("offlineAccountService")
public class OfflineAccountService
{
  @Resource(name="offlineAccountDAO")
  OfflineAccountDAO offlineAccountDAO;
  @Resource(name="commonService")
  private CommonService commonService;
  @Resource(name="offlineIdentityCardHisService")
  private OfflineIdentityCardHisService offlineIdentityCardHisService;
  
  public String getOfflineAccountReportDir()
  {
    return this.commonService.getDownLoadDir();
  }
  
  public List<OfflineAccountVO> listAllOfflineAccount()
  {
    List<OfflineAccountVO> offlineAccountList = null;
    try
    {
      offlineAccountList = this.offlineAccountDAO.listAllOfflineAccount();
    }
    catch (Exception e)
    {
      LogUtil.error("查询离线现金卡对象异常", e);
      offlineAccountList = new ArrayList();
    }
    return offlineAccountList;
  }
  
  public boolean insertOfflineAccount(OfflineAccountVO offlineAccountVO)
  {
    boolean excuteFlag = false;
    try
    {
      int i = this.offlineAccountDAO.insertOfflineAccount(offlineAccountVO);
      excuteFlag = i > 0;
      if (excuteFlag) {
        this.offlineIdentityCardHisService.insertIdentityAccount(offlineAccountVO);
      }
    }
    catch (Exception e)
    {
      LogUtil.error(" 插入离线现金卡对象异常--入参：" + offlineAccountVO.toString(), e);
    }
    return excuteFlag;
  }
  
  public OfflineAccountVO generateOfflineAccount(CardNoProduct product, String batNo)
  {
    OfflineAccountVO offlineAccountVO = new OfflineAccountVO();
    offlineAccountVO.setCardBin(product.getBin());
    offlineAccountVO.setCardType(product.getCardType());
    offlineAccountVO.setMerchantCode(product.getMerchantCode());
    offlineAccountVO.setAreaCode(product.getArea());
    offlineAccountVO.setCheckCode(product.getCheckCode());
    offlineAccountVO.setCardNo(product.getCardNo());
    offlineAccountVO.setBatNo(batNo);
    offlineAccountVO.setAcctBal(new BigDecimal("0.00"));
    offlineAccountVO.setInitDate(DateUtil.getDBCurrentTime());
    offlineAccountVO.setInitOper(this.offlineAccountDAO.getSystemCurrentOper());
    offlineAccountVO.setState(DzConstants.cardState.UN_ACTIVATE.value());
    return offlineAccountVO;
  }
  
  public boolean activateCard(OfflineAccountVO offlineAccountVO)
    throws Exception
  {
    try
    {
      int n = this.offlineAccountDAO.activateCard(offlineAccountVO);
      if (n > 0)
      {
        this.offlineIdentityCardHisService.insertIdentityAccount(offlineAccountVO);
        return true;
      }
      return false;
    }
    catch (Exception e)
    {
      LogUtil.error("激活离线卡异常，参数offlineAccountVO为：" + offlineAccountVO, e);
      throw e;
    }
  }
  
  public boolean bacthUpdateOfflineAccount(List<OfflineAccountVO> offlineAccountList)
  {
    boolean excuteFlag = true;
    try
    {
      int[] iArray = this.offlineAccountDAO.batchUpateOfflineAccount(offlineAccountList);
      for (int i : iArray) {
        if (i < 0)
        {
          excuteFlag = false;
          throw new BusinessException();
        }
      }
      this.offlineIdentityCardHisService.batchInsertOfflineIdentityCardHis(offlineAccountList);
    }
    catch (Exception e)
    {
      LogUtil.error(" 批量更新离线现金卡对象异常--入参：" + offlineAccountList.toString(), e);
    }
    return excuteFlag;
  }
  
  public List<OfflineAccountVO> queryOfflineAccount(OfflineAccountVO offlineAccountVO)
  {
    List<OfflineAccountVO> offlineAccountList = null;
    try
    {
      offlineAccountList = this.offlineAccountDAO.queryOfflineAccount(offlineAccountVO);
    }
    catch (Exception e)
    {
      LogUtil.error("根据传入条件查询离线现金卡异常", e);
      offlineAccountList = new ArrayList();
    }
    return offlineAccountList;
  }
  
  public List<OfflineAccountVO> queryOfflineAccountAndConvert(OfflineAccountVO offlineAccountVO)
  {
    List<OfflineAccountVO> offlineAccountList = null;
    try
    {
      offlineAccountList = this.offlineAccountDAO.queryOfflineAccountAndConvert(offlineAccountVO);
    }
    catch (Exception e)
    {
      LogUtil.error("根据传入条件查询离线现金卡异常", e);
      offlineAccountList = new ArrayList();
    }
    return offlineAccountList;
  }
  
  public boolean updatetOfflineAccountBal(OfflineAccountVO param)
    throws Exception
  {
    try
    {
      return this.offlineAccountDAO.updatetOfflineAccountBal(param);
    }
    catch (Exception e)
    {
      LogUtil.error("updatetOfflineAccountBal::更新离线卡记账余额异常，参数为：" + param, e);
      throw e;
    }
  }
}
